#!/bin/sh

declare APP_NAME=$1

if [ ! -n "${APP_NAME}" ]; then
  echo " APP_NAME is empty, exit."
  exit 0
else
  echo " APP_NAME is ${APP_NAME}, continue..."
fi

echo ""
echo "${APP_NAME} shuting down..."
echo ""

declare path=$(cd `dirname $0`; pwd)
echo "current path is  :" ${path} 


declare PID_FILE=${path}/${APP_NAME}.pid;

declare PID=$(cat ${PID_FILE})


if [[ ! $PID ]]; then
  echo "pid is null"
  echo "shut down completed."
else
  echo "killing pid: ${PID}"
  kill $PID
  echo "killed pid: ${PID}"
  PID=$(pgrep -f /apps/server/${APP_NAME}-server)

  if [[ ! $PID ]]; then
    echo "shuted down ok."
  else
    echo "shuted down fail."
    echo "kill -9 again pid: ${PID}"
    kill -9 $PID
    echo "killed -9 pid: ${PID}"
  fi
fi
